﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormMesWorkReport
    Public usr As String
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim objXjPgd As New clsXjPgd
    Dim objPgdMes As New clsPgdMes
    Dim objPgdMesWorkReport As New clsPgdMesWorkReport
    Dim objJPck As New clsJPck
    Dim httpHelper As New HttpHelper
    Dim txt As TextBox
    Private Sub FormMesWorkReport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim startDate As String = Date.Now.ToShortDateString()
        txtStartDate.Text = startDate
        Dim endDate As String = Date.Now.ToShortDateString()
        txtEndDate.Text = endDate
        setDgvData()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        setDgvData()
    End Sub

    Private Sub setDgvData()
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            Dim dt As DataTable = objXjPgd.getXjPgd(2, txtStartDate.Text.Trim(), txtEndDate.Text.Trim(), chkFinishCount.Checked)
            If dt.Rows.Count > 0 Then
                dt.Columns.Add("validity_name")
                For Each row As DataRow In dt.Rows
                    If row("validity") = "0" Then
                        row("validity_name") = "无效"
                    Else
                        row("validity_name") = "有效"
                    End If
                Next
            End If
            dgv.AutoGenerateColumns = False
            dgv.DataSource = dt
        Else
            MsgBox("请选择开始时间和结束时间", MsgBoxStyle.Information, "提示")
        End If
    End Sub

    Private Sub dgv_SelectionChanged(sender As Object, e As EventArgs) Handles dgv.SelectionChanged
        If dgv.SelectedRows.Count > 0 Then
            cmb_Type_JP.Items.Clear()
            cmb_Type_JP.Items.Add(dgv.SelectedRows(0).Cells("ColSpec").Value.ToString())
            cmb_Type_JP.SelectedIndex = 0
            txt_count_JP.Value = Decimal.Parse(dgv.SelectedRows(0).Cells("ColOperCount").Value.ToString())
            txt_finish_count_JP.Value = Decimal.Parse(dgv.SelectedRows(0).Cells("Col_finish_count").Value.ToString())
        End If
    End Sub

    Private Sub btnMesPlan_Click(sender As Object, e As EventArgs) Handles btnMesPlan.Click
        If String.IsNullOrEmpty(txtStartDate.Text.Trim()) = False And String.IsNullOrEmpty(txtEndDate.Text.Trim()) = False Then
            'Dim params As String = "{""workshop"":""出片"",""date"":""" & "2021-12-08" & """,""compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""}"
            Dim params As String = "{""workshop"":""出片"",""startdate"":""" & DateTime.Parse(txtStartDate.Text.Trim()).ToString("yyyy-MM-dd") & """,""enddate"":""" & DateTime.Parse(txtEndDate.Text.Trim()).ToString("yyyy-MM-dd") & """,""compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""}"
            Dim rtn As String = httpHelper.HttpPostWebService("findDispatchOrderInfoListByTimeAndCJ", params)
            'MsgBox(rtn)
            Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
            Dim code As String = jo("code").ToString() '输出0
            'MsgBox(code)
            Dim data As String = jo("data").ToString() '输出0
            'data = "[{""dispatchingorder"":""PGDH211103001"",
            '""start_time"":""2021-11-02 00:00:00"",
            '""end_time"":""2021-11-02 23:59:59"",
            '""project_code"":""fgm-test-0001"",
            '""project_name"":""fgm-test-0001"",
            '""equip_id"":""070046,070023,070020,070011,"",
            '""jp_type"":""mes不存在"",
            '""prod_spec_id"":""HDR-0.4-201-035-18"",""count"":9.0},
            '{""dispatchingorder"":""PGDH211103222"",""start_time"":""2021-11-02 00:00:00"",
            '""end_time"":""2021-11-02 23:59:59"",
            '""project_code"":""fgm-test-0001"",
            '""project_name"":""fgm-test-0001"",
            '""equip_id"":""070046,070023,070020,070011,"",
            '""jp_type"":""mes不存在"",
            '""prod_spec_id"":""HDR-0.4-201-035-18"",""count"":9.0}]"
            'MsgBox(data)
            Dim jarray As JArray = JArray.Parse(data)
            If jarray.Count > 0 Then
                For index = 0 To jarray.Count - 1
                    Dim en As New entityPgdMes
                    en.pgd_code = jarray(index)("dispatchingorder")
                    en.oper_date = DateTime.Parse(jarray(index)("start_time")).ToShortDateString()
                    en.start_time = jarray(index)("start_time").ToString()
                    en.end_time = DateTime.Parse(jarray(index)("start_time")).AddDays(1).ToString()
                    en.support_model = jarray(index)("p_prod_spec_id")
                    en.type_value = jarray(index)("prod_spec_id")
                    en.mes_project_code = jarray(index)("project_code")
                    en.mes_project_name = jarray(index)("project_name")
                    en.oper_count = jarray(index)("count")
                    en.workshop_id = 2
                    en.validity = "1"
                    en.status = "1"
                    en.origin = "0"
                    en.Ope = usr
                    en.jp_type = String.Empty
                    en.note = String.Empty
                    Dim dtmesdata As DataTable = objPgdMes.getListByPgd(jarray(index)("dispatchingorder").ToString())
                    If dtmesdata.Rows.Count = 0 Then
                        Dim rtn_save As Integer = objPgdMes.insertxjORyzz(en)
                    Else
                        Dim rtn_save As Integer = objPgdMes.updatexjORyzz(en)
                    End If
                Next
                setDgvData()
            Else
                MsgBox("没有数据", MsgBoxStyle.Information, "提示")
            End If
        Else
            MsgBox("请选择开始时间和结束时间", MsgBoxStyle.Information, "提示")
        End If
    End Sub

    Private Sub txt_MouseDown(sender As Object, e As MouseEventArgs) Handles txt_finish_count_JP.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub
    Private Sub btn_Save_Click(sender As Object, e As EventArgs) Handles btn_Save.Click
        If String.IsNullOrEmpty(usr) Then
            MsgBox("请勾选用户")
            Exit Sub
        End If
        If dgv.SelectedRows.Count <= 0 Then
            MsgBox("请选择生产计划")
            Exit Sub
        End If
        'If txt_finish_count_JL.Value > txt_count_JL.Value Then
        '    MsgBox("完成数量大于计划数量！", vbYes, "提示")
        '    Exit Sub
        'End If
        If txt_finish_count_JP.Value <= 0 Then
            MsgBox("完成数量需大于0", vbYes, "提示")
            Exit Sub
        End If
        If MsgBox("确认要进行保存吗？", vbYesNo, "提示") = vbYes Then
            Dim b_PgdMesProjectCode As String = dgv.SelectedRows(0).Cells("mes_project_code").Value.ToString()
            Dim typp As String = dgv.SelectedRows(0).Cells("Col_type").Value.ToString()
            Dim typp_val As String = dgv.SelectedRows(0).Cells("Coltype_value").Value.ToString()
            If typp.Contains("保护胶") Then
                objJPck.insert(String.Empty, "1", typp_val, txt_finish_count_JP.Value.ToString(), String.Empty, usr, "1")
            Else
                objJPck.insert(String.Empty, "0", typp_val, txt_finish_count_JP.Value.ToString(), String.Empty, usr, "1")
            End If
            Dim b_SysId As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
            Dim rtn As Integer = objXjPgd.updateFinishCount(txt_finish_count_JP.Value.ToString(), b_SysId)
            If rtn <> -2 Then
                Dim dtPgdMesWorkReport As DataTable = objPgdMesWorkReport.getListByPgdmesSysId("ok", b_SysId)
                If dtPgdMesWorkReport.Rows.Count > 0 Then
                    If MsgBox("此生产记录已报工过，确认要进行再次合格报工吗？", vbYesNo, "提示") = vbYes Then
                        objPgdMesWorkReport.update("ok", b_SysId, txt_finish_count_JP.Text.Trim(), usr)
                    End If
                Else
                    If MsgBox("确认要进行合格报工吗？", vbYesNo, "提示") = vbYes Then
                        objPgdMesWorkReport.insert("ok", b_SysId, txt_finish_count_JP.Text.Trim(), usr)
                    End If
                End If
                Dim rtn_mes_bg As String = mesbg(txt_finish_count_JP.Text.Trim(), typp_val, b_PgdMesProjectCode)
                '{"project_code":"GⅡ型700支座-结构七-S-CL-002","qualifiednumber":"6","unqualifiednumber":"0","productorname":"02.04.02.401","procedurename":"出片","compid":"0de02b54-4e55-4dbf-ab89-f785df5f405c","unqualifiedReason":""}
                '{"msg":"查询不到对应的报工数据,请检查参数!","code":1}
                Dim jo As JObject = JsonConvert.DeserializeObject(rtn_mes_bg)
                Dim code As String = jo("code").ToString() '输出0
                Dim msg As String = jo("msg").ToString() '输出操作成功
                If code.Equals("0") Then
                    MsgBox("保存成功，MES报工成功", vbYes, "提示")
                Else
                    MsgBox("保存成功，MES报工失败：" & msg, vbYes, "提示")
                End If
                setDgvData()
            Else
                MsgBox("保存失败", vbYes, "提示")
            End If
        End If
    End Sub

    Private Function mesbg(oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "出片" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Return httpHelper.HttpPostWebService("dispatchingOrderBG", params)
    End Function

    Private Sub btnRkMes_Click(sender As Object, e As EventArgs) Handles btnRkMes.Click
        Dim b_SysId As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
        Dim b_PgdMesProjectCode As String = dgv.SelectedRows(0).Cells("mes_project_code").Value.ToString()
        Dim b_PgdMes_gx As String = dgv.SelectedRows(0).Cells("Coltype_value").Value.ToString()
        Dim warehousecode As String = "ZAGZ09"
        Dim rnt_int As Integer = mesRk(txt_finish_count_JP.Text, warehousecode, b_PgdMes_gx, b_PgdMesProjectCode)
    End Sub

    Private Function mesRk(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As Integer
        Dim rtn_int As Integer = 0
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "出片" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103102""" '部门id
        params &= "}"
        Dim rtn As String = httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
        Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
        Dim code As String = jo("code").ToString() '输出0
        Dim msg As String = jo("msg").ToString() '输出操作成功
        If code.Equals("0") And msg.Equals("操作成功") Then
            rtn_int = 1
        End If
        Return rtn_int
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Dim b_SysId As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
        Dim b_PgdMesProjectCode As String = "GⅡ型700支座-结构七-S-CL-001"
        Dim b_PgdMes_gx As String = "02.02.023yz"
        Dim warehousecode As String = "GZYZZK"
        Dim rnt_int As String = mesRkYzz("1", warehousecode, b_PgdMes_gx, b_PgdMesProjectCode)
        Dim ss As String = rnt_int
    End Sub

    Private Function mesRkYzz(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "预组装" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103105""" '部门id
        params &= "}"
        Return httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
    End Function

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Dim b_SysId As String = dgv.SelectedRows(0).Cells("sys_id").Value.ToString()
        Dim b_PgdMesProjectCode As String = "GⅡ型700支座-结构七-S-CL-001"
        Dim b_PgdMes_gx As String = "02.02.023"
        Dim warehousecode As String = "ZAGZ07"
        Dim rnt_int As String = mesRkLh("1", warehousecode, b_PgdMes_gx, b_PgdMesProjectCode)
        Dim ss As String = rnt_int
    End Sub

    Private Function mesRkLh(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "硫化" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103104""" '部门id
        params &= "}"
        Return httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
    End Function



    Private Sub txt_MouseClick(sender As Object, e As MouseEventArgs) Handles txtStartDate.MouseClick, txtEndDate.MouseClick
        txt = CType(sender, TextBox)
        DateNavigator1.Left = txt.Left
        DateNavigator1.Top = txt.Location.Y + txt.Height
        DateNavigator1.Visible = True
    End Sub

    Private Sub DateNavigator1_DateTimeChanged(sender As Object, e As EventArgs) Handles DateNavigator1.DateTimeChanged
        txt.Text = DateNavigator1.DateTime.ToShortDateString()
        DateNavigator1.Visible = False
    End Sub

End Class